package de.itagile.hypersphere

class EntityRelationshipTemplate extends Template {

	def rootRelationship() {
		return {relationship ->
			def name = toFirstLower(extractClass(relationship.fqn))
			def returnType = getReturnType(relationship)
			[
			 relationshipField(returnType, name),
			 relationshipGetter(relationship.type, returnType, name),
			 relationshipSetter(relationship.type, returnType, name),
			].flatten()
		}
	}
	
	def getReturnType(def relationship) {
		if (relationship.type.startsWith("Many"))
			return"${relationship.collection}<${relationship.fqn}>"
		else
			return relationship.fqn
	}
	
	def relationshipField(def returnType, def name) {
		["private $returnType $name;"]
	}

	def relationshipGetter(type, returnType, name) {
		[
		 	"@${type}",
		 	"public $returnType get${toFirstUpper(name)}() {",
		 		"return $name;",
		 	"}"
		]
	}
	
	def relationshipSetter(type, returnType, name) {
		[
		 	"public void set${toFirstUpper(name)}($returnType $name) {",
		 		"this.$name = $name;",
		 	"}"
		]
	}
}